package com.lkm.app_ui.ui.sale.p;

import android.os.Handler;

import com.lkm.app_ui.RunTimeData;
import com.lkm.app_ui.log.AppLogger;
import com.lkm.app_ui.m.order_img.OnGoodImageTackEvent;
import com.lkm.app_ui.ui.sale.SaleContract;
import com.lkm.comlib.help.ViewUIHelp;
import com.lkm.frame.otto.LinkBusCycleWrapper;
import com.lkm.frame.otto.ObserverAutoNext;
import com.lkm.frame.task2.BackCallAdapter;
import com.lkm.frame.task2.Task;
import com.lkm.frame.util.CycleHelp;
import com.lkm.javatool.StringHelp;
import com.lkm.javatool.lang.RunnablePR;
import com.lkm.market2_0.m.steelyardserver.event.OnSubtotalEvent;
import com.lkm.mvp.FullLifecycleObserverA;

class GoodImagePresenter extends FullLifecycleObserverA implements SaleContract.GoodImagePresenter {

    private Handler mainHandler;
    private CycleHelp cycleHelp;
    private SaleContract.SubtotalPresenter subtotalPresenter;
    private SaleContract.GoodImageView goodImageView;


    public GoodImagePresenter(Handler mainHandler, CycleHelp cycleHelp) {
        this.mainHandler = mainHandler;
        this.cycleHelp = cycleHelp;


    }

    void binP(SaleContract.SubtotalPresenter subtotalPresenter) {
        this.subtotalPresenter = subtotalPresenter;
    }

    @Override
    public void binView(SaleContract.GoodImageView goodImageView) {
        if (goodImageView == null) return;
        if (this.goodImageView == null) {
            this.goodImageView = goodImageView;
            reOnGoodImageTackEvent();
            reOnSubtotalEvent();
        }
        this.goodImageView = goodImageView;

    }

    private RunnablePR<String, Void> pr;

    @Override
    public void setOnTackGoodImage(RunnablePR<String, Void> pr) {
        this.pr = pr;
    }


    private void reOnGoodImageTackEvent() {
        LinkBusCycleWrapper.register(new ObserverAutoNext<OnGoodImageTackEvent>() {
            private void postonReceiver(OnGoodImageTackEvent data) {



                if (!isExit()) {
                    boolean isP = subtotalPresenter.hasGoodEntity(data.goodEntity.itemId);

                    if (!isExit() && isP) {
                        goodImageView.showTackGoodImage(data.imgPath);

                        if (pr != null)
                            pr.run(data.imgPath);
                    }
                }
            }

            @Override
            public void onReceiver(OnGoodImageTackEvent data) {
                mainHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        postonReceiver(data);
                    }
                });
            }

            @Override
            public Class<OnGoodImageTackEvent> getBusEventClass() {
                return OnGoodImageTackEvent.class;
            }
        }, cycleHelp, CycleHelp.CycleType.Destroy);
    }


    private void reOnSubtotalEvent() {
        LinkBusCycleWrapper.register(new ObserverAutoNext<OnSubtotalEvent>() {
            @Override
            public Class<OnSubtotalEvent> getBusEventClass() {
                return OnSubtotalEvent.class;
            }

            @Override
            public void onReceiver(OnSubtotalEvent data) {
                AppLogger.Order.onSubtotalToUI();

            }
        }, cycleHelp, CycleHelp.CycleType.Destroy);
    }
}
